package com.ios.controller;

import com.ios.dto.DisablerExecution;
import com.ios.dto.RelativeExecution;
import com.ios.entity.DisablerUser;
import com.ios.entity.RelativeUser;
import com.ios.enums.DisablerStateEnum;
import com.ios.enums.RelativeStateEnum;
import com.ios.exceptions.DisableOperationException;
import com.ios.exceptions.RelativeOperationException;
import com.ios.service.IDisablerService;
import com.ios.service.IRelativeService;
import com.ios.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/edit",method = {RequestMethod.POST})
public class EditInfoController {
    @Autowired
    private IDisablerService disablerService;
    @Autowired
    private IRelativeService relativeService;

    @RequestMapping(value = "/disabler")
    @ResponseBody
    public Map<String,Object> editDisabler(HttpServletRequest request) throws ParseException {
        Map<String,Object> modelMap = new HashMap<String,Object>();
        DisablerUser disablerUser = new DisablerUser();
        /*接收并转化相应的参数,残疾人用户个人信息以及头像信息*/
        int id = HttpServletRequestUtil.getInt(request,"id");
        String username = HttpServletRequestUtil.getString(request, "username");
        String password = HttpServletRequestUtil.getString(request, "password");
        String name = HttpServletRequestUtil.getString(request, "name");
        String sex = HttpServletRequestUtil.getString(request, "sex");
        String birthdayStr =  HttpServletRequestUtil.getString(request, "birthday");
        String firstcontact = HttpServletRequestUtil.getString(request, "first_contact");
        String phone = HttpServletRequestUtil.getString(request, "phone_no");
        disablerUser.setId(id);
        disablerUser.setUsername(username);
        disablerUser.setPassword(password);
        disablerUser.setName(name);
        disablerUser.setSex(sex);
        disablerUser.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthdayStr));
        disablerUser.setFirstContact(firstcontact);
        disablerUser.setPhoneNo(phone);
        MultipartResolver MultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartHttpServletRequest =MultipartResolver.resolveMultipart(request);
        MultipartFile HeadImg = multipartHttpServletRequest.getFile("HeadImg");
        /*用户信息修改*/
        if(disablerUser!= null){
            try{
                DisablerExecution de = disablerService.EditDisablerUserInfo(disablerUser,HeadImg.getInputStream(),HeadImg.getOriginalFilename());
                if(de.getState() == DisablerStateEnum.SUCCESS.getState()){
                    modelMap.put("success",true);
                    modelMap.put("editedInfo",de.getDisablerUser());
                }else {
                    modelMap.put("success",false);
                    modelMap.put("errMsg",de.getStateInfo());
                }
            }catch (IOException e){
                    modelMap.put("success",false);
                    modelMap.put("errMsg5",e.getMessage());
                }
            }
        return modelMap;
    }

    @RequestMapping(value = "/relative")
    @ResponseBody
    private Map<String,Object> editRelative(HttpServletRequest request) throws ParseException {
        Map<String,Object> modelMap = new HashMap<String,Object>();
        /*接收并转化相应的参数,监护人用户个人信息以及头像信息*/
        RelativeUser relativeUser = new RelativeUser();
        /*接收并转化相应的参数,残疾人用户个人信息以及头像信息*/
        int id = HttpServletRequestUtil.getInt(request,"id");
        String username = HttpServletRequestUtil.getString(request, "username");
        String password = HttpServletRequestUtil.getString(request, "password");
        String name = HttpServletRequestUtil.getString(request, "name");
        String sex = HttpServletRequestUtil.getString(request, "sex");
        String birthdayStr =  HttpServletRequestUtil.getString(request, "birthday");
        Integer disablerid = HttpServletRequestUtil.getInt(request, "disabler_id");
        String phone = HttpServletRequestUtil.getString(request, "phone_no");
        relativeUser.setId(id);
        relativeUser.setUsername(username);
        relativeUser.setPassword(password);
        relativeUser.setName(name);
        relativeUser.setSex(sex);
        relativeUser.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthdayStr));
        relativeUser.setDisablerId(disablerid);
        relativeUser.setPhoneNo(phone);
        MultipartResolver MultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartHttpServletRequest =MultipartResolver.resolveMultipart(request);
        MultipartFile relativeHeadImg = multipartHttpServletRequest.getFile("relativeHeadImg");
        /*用户信息修改*/
        if(relativeUser != null){
            try{
                RelativeExecution re = relativeService.EditRelativeInfo(relativeUser,relativeHeadImg.getInputStream(),relativeHeadImg.getOriginalFilename());
                if(re.getState() == RelativeStateEnum.SUCCESS.getState()){
                    modelMap.put("success",true);
                    modelMap.put("editedInfo",re.getRelativeUser());
                }else {
                    modelMap.put("success",false);
                    modelMap.put("errMsg",re.getStateInfo());
                }
            }catch (IOException e){
                    modelMap.put("success",false);
                    modelMap.put("errMsg",e.getMessage());
                }
            }
        return modelMap;
    }
}
